<template>
  <el-dialog v-model="detectionEditWinDialogVisible"
             title="修改检测项" width="50%">
    <template #default>
      <el-form v-model="editDetection" label-width="auto" inline="true">
        <el-form-item label="检测项编码">
          <el-input readonly v-model="editDetection.detectionId"/>
        </el-form-item>
        <el-form-item label="检测项名称">
          <el-input v-model="editDetection.detectionName"/>
        </el-form-item>
      </el-form>
      <el-form v-model="editDetection" label-width="auto" inline="true">
        <el-form-item label="检测项类型">
              <el-input v-model="editDetection.detectionType"/>
        </el-form-item>
        <el-form-item label="检测工具">
              <el-input v-model="editDetection.detectionTool"/>
        </el-form-item>
      </el-form>
    </template>

    <template #footer>
      <div class="dialog-footer">
        <el-button @click="cancelEdit">取消</el-button>
        <el-button @click="editSubmit">确定</el-button>
      </div>
    </template>

  </el-dialog>
</template>
<script setup>
import {onMounted, ref , watch} from 'vue'
import {ElMessage} from "element-plus";
import DetectionRequest from "@/api/detection.js";

//声明这个子组件
const emits = defineEmits(['editCompletesS','cancelEdit']);
//申明这个子组件的属性
const props = defineProps(['detectionId']);
//弹窗标记
let detectionEditWinDialogVisible = ref(false);
//编辑的项目对象
let editDetection = ref({});

//取消编辑按钮事件
function cancelEdit(){
  editDetection = ref({});
  detectionEditWinDialogVisible.value = false;
  // 触发暴漏出去的取消编辑的事件
  emits("cancelEdit")

}
//提交事件
function editSubmit(){
  DetectionRequest.updateDefectById(editDetection.value,res=>{
    if(res.code == 200){
      ElMessage({
        type:"success",
        message:"编辑成功"
      });
      // 触发编辑完成的事件
      emits('editCompletesS');
      // 关闭弹窗
      cancelEdit();
    }else{
      ElMessage({
        type:"warning",
        message:"项目编辑失败"
      });
    }
  })
}

//加载项目列表
function loadDetections(){
  DetectionRequest.selectById(props.detectionId, res=>{
    if (res.data){
      //加载数据
      editDetection.value = res.data;
      console.log(editDetection.value);
      //显示弹窗
      detectionEditWinDialogVisible.value = true;
    }
  })
}
//侦听器
watch(()=>props.detectionId,()=>{
  //当props中的defectId发生变化时，就重新加载数据
  if(props.detectionId){
    loadDetections();
  }
})

onMounted( ()=>{
  loadDetections();
})



</script>